//
//  TSInvationViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/21.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSInvationViewController: TSBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavigationTitle(title: "邀请好友")
        
        
        let img = UIImageView.init()
        img.image = creatQRCodeImage(text: "AAAA", WH: SCREEN_WIDTH - 2*1)
        self.view.addSubview(img)
        img.snp.makeConstraints { (make) in
            make.centerX.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*15, height: SCREEN_WIDTH - 2*15))
        }
    }

}

extension TSInvationViewController {
    
    //MARK:- 生成二维码
    func creatQRCodeImage(text: String, WH: CGFloat) -> UIImage{
        
        //创建滤镜
        let filter = CIFilter(name: "CIQRCodeGenerator")
        //还原滤镜的默认属性
        filter?.setDefaults()
        //设置需要生成二维码的数据
        filter?.setValue(text.data(using: String.Encoding.utf8), forKey: "inputMessage")
        //从滤镜中取出生成的图片
        let ciImage = filter?.outputImage
        //这个清晰度好
        let bgImage = createNonInterpolatedUIImageFormCIImage(image: ciImage!, size: WH)
        
        return bgImage
    }


    func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        let extent: CGRect = image.extent.integral
        let scale: CGFloat = min(size/extent.width, size/extent.height)
        
        let width = extent.width * scale
        let height = extent.height * scale
        let cs: CGColorSpace = CGColorSpaceCreateDeviceGray()
        let bitmapRef = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: cs, bitmapInfo: 0)!
        
        let context = CIContext(options: nil)
        let bitmapImage: CGImage = context.createCGImage(image, from: extent)!
        
        bitmapRef.interpolationQuality = CGInterpolationQuality.none
        bitmapRef.scaleBy(x: scale, y: scale)
        bitmapRef.draw(bitmapImage, in: extent)
        let scaledImage: CGImage = bitmapRef.makeImage()!
        return UIImage(cgImage: scaledImage)
    }
}
